import { DirectiveBinding } from 'vue';

import { useUserStore } from '@/store';

function checkBtn(el: HTMLElement, binding: DirectiveBinding) {
  const { value } = binding;
  const userStore = useUserStore();
  const { userInfo } = userStore;

  if (value) {
    const hasPermission = userInfo.sysUser.id === value;
    if (!hasPermission && el.parentNode) {
      el.parentNode.removeChild(el);
    }
  } else {
    throw new Error(`Like v-btn="xxxxxxxxx"`);
  }
}

export default {
  mounted(el: HTMLElement, binding: DirectiveBinding) {
    checkBtn(el, binding);
  },
  updated(el: HTMLElement, binding: DirectiveBinding) {
    checkBtn(el, binding);
  },
};
